package com.setting.defbarchartutils;

import android.content.Context;
import android.graphics.Color;

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Fill;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.setting.deflinechartutils.DefLineChartSetting;
import com.setting.deflinechartutils.PointDataView;
import com.setting.view.DefBarChart;
import com.setting.view.DefLineChart;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DefBarChartSetting {
    private Context context;
    private DefBarChart defBarChart;
    //BarDataSet每一个对象就是一组数据
    private List<BarDataSet> set = new ArrayList<BarDataSet>();
    private Legend legend;
    private XAxis xAxis;
    private YAxis yAxis;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public DefBarChart getDefBarChart() {
        return defBarChart;
    }

    public void setDefBarChart(DefBarChart defBarChart) {
        this.defBarChart = defBarChart;
    }

    public List<BarDataSet> getSet() {
        return set;
    }

    public void setSet(List<BarDataSet> set) {
        this.set = set;
    }

    public Legend getLegend() {
        return legend;
    }

    public void setLegend(Legend legend) {
        this.legend = legend;
    }

    public XAxis getxAxis() {
        return xAxis;
    }

    public void setxAxis(XAxis xAxis) {
        this.xAxis = xAxis;
    }

    public YAxis getyAxis() {
        return yAxis;
    }

    public void setyAxis(YAxis yAxis) {
        this.yAxis = yAxis;
    }

    public DefBarChartSetting() {
    }

    public DefBarChartSetting(Context context, DefBarChart defBarChart) {
        this.context = context;
        this.defBarChart = defBarChart;
    }

    public static class SetAttribute{
        private DefBarChartSetting setting;
        private List<String> label;
        private int textColor = Color.BLACK;
        private float textsize = 17f;
        private int color = Color.rgb(140, 234, 255);
        private boolean drawvalues = true;
        private float barwidth = 0.2f;
        private PointDataView mv;
        private Fill fill = new Fill(color);

        public DefBarChartSetting.SetAttribute setting(DefBarChartSetting setting) {
            this.setting = setting;
            return this;
        }
        public DefBarChartSetting.SetAttribute label(List<String> label) {
            this.label = label;
            return this;
        }
        public DefBarChartSetting.SetAttribute textColor(int textColor) {
            this.textColor = textColor;
            return this;
        }
        public DefBarChartSetting.SetAttribute textsize(float textsize) {
            this.textsize = textsize;
            return this;
        }
        public DefBarChartSetting.SetAttribute color(int color) {
            this.color = color;
            return this;
        }
        public DefBarChartSetting.SetAttribute drawvalues(boolean drawvalues) {
            this.drawvalues = drawvalues;
            return this;
        }
        public DefBarChartSetting.SetAttribute barwidth(float barwidth) {
            this.barwidth = barwidth;
            return this;
        }

        public DefBarChartSetting.SetAttribute mv(PointDataView mv) {
            this.mv = mv;
            return this;
        }
        public DefBarChartSetting.SetAttribute fill(Fill fill) {
            this.fill = fill;
            return this;
        }
        public DefBarChartSetting.SetAttribute setData(List<BarEntry> list, int i) {
            //判断图表中原来是否有数据
            if (setting.defBarChart.getData() != null && setting.defBarChart.getData().getDataSetCount() > i) {
                //获取数据
                setting.set.add(i, (BarDataSet) setting.defBarChart.getData().getDataSetByIndex(i));
                setting.set.get(i).setValues(list);
                //刷新数据
                setting.defBarChart.getData().notifyDataChanged();
                setting.defBarChart.notifyDataSetChanged();
            } else {
                //设置数据：图例名称
                setting.set.add(i, new BarDataSet(list, this.label.get(i)));
                //值的颜色
                setting.set.get(i).setValueTextColor(this.textColor);
                //值的大小
                setting.set.get(i).setValueTextSize(this.textsize);
                //图例标签的颜色
                setting.set.get(i).setColor(this.color);
                //柱子渐变色
                setting.set.get(i).setFills(Collections.singletonList(this.fill));
                //是否显示值
                setting.set.get(i).setDrawValues(this.drawvalues);
                //格式化显示数据
                final DecimalFormat mFormat = new DecimalFormat("###,###,##0");
                setting.set.get(i).setValueFormatter(new IValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                        return mFormat.format(value);
                    }
                });

                //设置点击后柱子透明度改变
                setting.set.get(i).setHighLightAlpha(100);

                //保存LineDataSet集合
                ArrayList<IBarDataSet> dataSets = new ArrayList<>();

                // add the datasets
                dataSets.addAll(setting.set);

                //创建LineData对象 属于LineChart折线图的数据集合
                BarData data = new BarData(dataSets);

                //设置柱子的宽度
                data.setBarWidth(this.barwidth);

                //折线图点的标记
                setting.defBarChart.setMarker(this.mv);

                // 添加到图表中
                setting.defBarChart.setData(data);

                //绘制图表
                setting.defBarChart.invalidate();
            }
            return this;
        }

    }

    public static class SetChart{
        private DefBarChartSetting setting;
        private boolean descriptionenabled = true;
        private String descriptiontext = "Description Label";
        private String nodatatext = "No chart data available.";
        private int noDatatextcolor = Color.rgb(247, 189, 51);
        private boolean drawgridbackground = false;
        private boolean drawborders = false;
        private boolean touchenabled = true;
        private boolean scaleenabled = true;
        private boolean scalexenabled = true;
        private boolean scaleyenabled = true;
        private boolean pinchzoom = false;
        private boolean doubletaptozoomenabled = true;
        private float extrabottomoffset = 0.f;

        public DefBarChartSetting.SetChart setting(DefBarChartSetting setting) {
            this.setting = setting;
            return this;
        }
        public DefBarChartSetting.SetChart descriptionenabled(boolean descriptionenabled) {
            this.descriptionenabled = descriptionenabled;
            return this;
        }
        public DefBarChartSetting.SetChart descriptiontext(String descriptiontext) {
            this.descriptiontext = descriptiontext;
            return this;
        }
        public DefBarChartSetting.SetChart nodatatext(String nodatatext) {
            this.nodatatext = nodatatext;
            return this;
        }
        public DefBarChartSetting.SetChart noDatatextcolor(int noDatatextcolor) {
            this.noDatatextcolor = noDatatextcolor;
            return this;
        }
        public DefBarChartSetting.SetChart drawgridbackground(boolean drawgridbackground) {
            this.drawgridbackground = drawgridbackground;
            return this;
        }
        public DefBarChartSetting.SetChart drawborders(boolean drawborders) {
            this.drawborders = drawborders;
            return this;
        }
        public DefBarChartSetting.SetChart touchenabled(boolean touchenabled) {
            this.touchenabled = touchenabled;
            return this;
        }
        public DefBarChartSetting.SetChart scaleenabled(boolean scaleenabled) {
            this.scaleenabled = scaleenabled;
            return this;
        }
        public DefBarChartSetting.SetChart scalexenabled(boolean scalexenabled) {
            this.scalexenabled = scalexenabled;
            return this;
        }
        public DefBarChartSetting.SetChart scaleyenabled(boolean scaleyenabled) {
            this.scaleyenabled = scaleyenabled;
            return this;
        }
        public DefBarChartSetting.SetChart pinchzoom(boolean pinchzoom) {
            this.pinchzoom = pinchzoom;
            return this;
        }
        public DefBarChartSetting.SetChart doubletaptozoomenabled(boolean doubletaptozoomenabled) {
            this.doubletaptozoomenabled = doubletaptozoomenabled;
            return this;
        }
        public DefBarChartSetting.SetChart extrabottomoffset(float extrabottomoffset) {
            this.extrabottomoffset = extrabottomoffset;
            return this;
        }

        public void setChart() {
            //隐藏描述
            setting.defBarChart.getDescription().setEnabled(this.descriptionenabled);
            //设置图表描述信息
            setting.defBarChart.getDescription().setText(this.descriptiontext);
            //没有数据时显示的文字
            setting.defBarChart.setNoDataText(this.nodatatext);
            //没有数据时显示文字的颜色
            setting.defBarChart.setNoDataTextColor(this.noDatatextcolor);
            //chart 绘图区后面的背景矩形将绘制
            setting.defBarChart.setDrawGridBackground(this.drawgridbackground);
            //禁止绘制图表边框的线
            setting.defBarChart.setDrawBorders(this.drawborders);
            // 设置是否可以触摸
            setting.defBarChart.setTouchEnabled(this.touchenabled);
            //是否可以缩放 x和y轴, 默认为true
            setting.defBarChart.setScaleEnabled(this.scaleenabled);
            //是否可以缩放 仅x轴
            setting.defBarChart.setScaleXEnabled(this.scalexenabled);
            //是否可以缩放 仅y轴
            setting.defBarChart.setScaleYEnabled(this.scaleyenabled);
            //是否支持x、y轴同时缩放，默认为false
            setting.defBarChart.setPinchZoom(this.pinchzoom);
            //设置是否可以通过双击屏幕放大图表。默认为true
            setting.defBarChart.setDoubleTapToZoomEnabled(this.doubletaptozoomenabled);
            //距视图窗口底部的偏移，类似与paddingbottom
            setting.defBarChart.setExtraBottomOffset(this.extrabottomoffset);
        }
    }

    public static class SetLegend{
        private DefBarChartSetting setting;
        private boolean enabled = false;
        private boolean drawinside = false;
        private Legend.LegendVerticalAlignment verticalalignment = Legend.LegendVerticalAlignment.BOTTOM;
        private Legend.LegendHorizontalAlignment horizontalalignment = Legend.LegendHorizontalAlignment.LEFT;
        private Legend.LegendOrientation orientation = Legend.LegendOrientation.HORIZONTAL;
        private float textSize = 10f;
        private int textcolor = Color.BLACK;
        private Legend.LegendForm form = Legend.LegendForm.SQUARE;
        private float formsize = 8f;
        private boolean wordwrapenabled = false;
        private float formlinewidth = 3f;

        public DefBarChartSetting.SetLegend setting(DefBarChartSetting setting) {
            this.setting = setting;
            return this;
        }
        public DefBarChartSetting.SetLegend enabled(boolean enabled) {
            this.enabled = enabled;
            return this;
        }
        public DefBarChartSetting.SetLegend drawinside(boolean drawinside) {
            this.drawinside = drawinside;
            return this;
        }
        public DefBarChartSetting.SetLegend verticalalignment(Legend.LegendVerticalAlignment verticalalignment) {
            this.verticalalignment = verticalalignment;
            return this;
        }
        public DefBarChartSetting.SetLegend horizontalalignment(Legend.LegendHorizontalAlignment horizontalalignment) {
            this.horizontalalignment = horizontalalignment;
            return this;
        }
        public DefBarChartSetting.SetLegend orientation(Legend.LegendOrientation orientation) {
            this.orientation = orientation;
            return this;
        }
        public DefBarChartSetting.SetLegend textSize(float textSize) {
            this.textSize = textSize;
            return this;
        }
        public DefBarChartSetting.SetLegend textcolor(int textcolor) {
            this.textcolor = textcolor;
            return this;
        }
        public DefBarChartSetting.SetLegend form(Legend.LegendForm form) {
            this.form = form;
            return this;
        }
        public DefBarChartSetting.SetLegend formsize(float formsize) {
            this.formsize = formsize;
            return this;
        }
        public DefBarChartSetting.SetLegend wordwrapenabled(boolean wordwrapenabled) {
            this.wordwrapenabled = wordwrapenabled;
            return this;
        }
        public DefBarChartSetting.SetLegend formlinewidth(float formlinewidth) {
            this.formlinewidth = formlinewidth;
            return this;
        }

        public void setLegend() {
            //图例
            setting.legend = setting.defBarChart.getLegend();
            //是否显示图例
            setting.legend.setEnabled(this.enabled);
            //是否绘制在图表里面
            setting.legend.setDrawInside(this.drawinside);
            //设置图例的位置
            setting.legend.setVerticalAlignment(this.verticalalignment);
            setting.legend.setHorizontalAlignment(this.horizontalalignment);
            setting.legend.setOrientation(this.orientation);
            //设置文字大小
            setting.legend.setTextSize(this.textSize);
            //设置图例标签文本的颜色
            setting.legend.setTextColor(this.textcolor);
            //正方形，圆形或线
            setting.legend.setForm(this.form);
            // 设置Form的大小
            setting.legend.setFormSize(this.formsize);
            //是否支持自动换行 目前只支持BelowChartLeft, BelowChartRight, BelowChartCenter
            setting.legend.setWordWrapEnabled(this.wordwrapenabled);
            //设置Form的宽度
            setting.legend.setFormLineWidth(this.formlinewidth);
        }

    }

    public static class SetXAxis{
        private DefBarChartSetting setting;
        private float labelrotationangle = 0f;
        private boolean drawgridlines = true;
        private int axislinecolor = Color.GRAY;
        private float axislinewidth = 1f;
        private XAxis.XAxisPosition position = XAxis.XAxisPosition.TOP;
        private float axismaximum = 0f;
        private float axisminimum = 0f;
        private int labelcount = 6;
        private boolean labelforce = false;
        private int textcolor = Color.BLACK;


        public DefBarChartSetting.SetXAxis setting(DefBarChartSetting setting) {
            this.setting = setting;
            return this;
        }
        public DefBarChartSetting.SetXAxis labelrotationangle(float labelrotationangle) {
            this.labelrotationangle = labelrotationangle;
            return this;
        }
        public DefBarChartSetting.SetXAxis drawgridlines(boolean drawgridlines) {
            this.drawgridlines = drawgridlines;
            return this;
        }
        public DefBarChartSetting.SetXAxis axislinecolor(int axislinecolor) {
            this.axislinecolor = axislinecolor;
            return this;
        }
        public DefBarChartSetting.SetXAxis axislinewidth(float axislinewidth) {
            this.axislinewidth = axislinewidth;
            return this;
        }
        public DefBarChartSetting.SetXAxis position(XAxis.XAxisPosition position) {
            this.position = position;
            return this;
        }
        public DefBarChartSetting.SetXAxis axismaximum(float axismaximum) {
            this.axismaximum = axismaximum;
            return this;
        }
        public DefBarChartSetting.SetXAxis axisminimum(float axisminimum) {
            this.axisminimum = axisminimum;
            return this;
        }
        public DefBarChartSetting.SetXAxis labelcount(int labelcount,boolean labelforce) {
            this.labelcount = labelcount;
            this.labelforce = labelforce;
            return this;
        }
        public DefBarChartSetting.SetXAxis textcolor(int textcolor) {
            this.textcolor = textcolor;
            return this;
        }

        public void setXAxis() {
            //X轴
            setting.xAxis = setting.defBarChart.getXAxis();
            //旋转60度
            setting.xAxis.setLabelRotationAngle(this.labelrotationangle);
            //是否绘制X轴上的网格线（背景里面的竖线）
            setting.xAxis.setDrawGridLines(this.drawgridlines);
            //X轴颜色
            setting.xAxis.setAxisLineColor(this.axislinecolor);
            //X轴粗细
            setting.xAxis.setAxisLineWidth(this.axislinewidth);
            //X轴所在位置   默认为上面
            setting.xAxis.setPosition(this.position);
            //X轴最大数值
            setting.xAxis.setAxisMaximum(this.axismaximum);
            //X轴最小数值
            setting.xAxis.setAxisMinimum(this.axisminimum);
            //X轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题
            setting.xAxis.setLabelCount(this.labelcount, this.labelforce);
            //设置字体颜色
            setting.xAxis.setTextColor(this.textcolor);
            //设置最小的区间，避免标签的迅速增多
            setting.xAxis.setGranularity(1f);
        }

    }

    public static class SetYAxis{
        private DefBarChartSetting setting;
        private boolean drawgridlines = true;
        private int axislinecolor = Color.GRAY;
        private float axislinewidth = 1f;
        private float axismaximum = 0f;
        private float axisminimum = 0f;
        private int textcolor = Color.BLACK;
        private int labelcount = 6;
        private boolean labelforce = false;
        private boolean axisrightenabled = true;

        public DefBarChartSetting.SetYAxis setting(DefBarChartSetting setting) {
            this.setting = setting;
            return this;
        }
        public DefBarChartSetting.SetYAxis drawgridlines(boolean drawgridlines) {
            this.drawgridlines = drawgridlines;
            return this;
        }
        public DefBarChartSetting.SetYAxis axislinecolor(int axislinecolor) {
            this.axislinecolor = axislinecolor;
            return this;
        }
        public DefBarChartSetting.SetYAxis axislinewidth(float axislinewidth) {
            this.axislinewidth = axislinewidth;
            return this;
        }
        public DefBarChartSetting.SetYAxis axismaximum(float axismaximum) {
            this.axismaximum = axismaximum;
            return this;
        }
        public DefBarChartSetting.SetYAxis axisminimum(float axisminimum) {
            this.axisminimum = axisminimum;
            return this;
        }
        public DefBarChartSetting.SetYAxis textcolor(int textcolor) {
            this.textcolor = textcolor;
            return this;
        }
        public DefBarChartSetting.SetYAxis labelcount(int labelcount,boolean labelforce) {
            this.labelcount = labelcount;
            this.labelforce = labelforce;
            return this;
        }
        public DefBarChartSetting.SetYAxis axisrightenabled(boolean axisrightenabled) {
            this.axisrightenabled = axisrightenabled;
            return this;
        }
        public void setYAxis(){
            //Y轴
            setting.yAxis = setting.defBarChart.getAxisLeft();
            //是否绘制Y轴上的网格线（背景里面的横线）
            setting.yAxis.setDrawGridLines(this.drawgridlines);
            //Y轴颜色
            setting.yAxis.setAxisLineColor(this.axislinecolor);
            //Y轴粗细
            setting.yAxis.setAxisLineWidth(this.axislinewidth);
            //Y轴最大数值
            setting.yAxis.setAxisMaximum(this.axismaximum);
            //Y轴最小数值
            setting.yAxis.setAxisMinimum(this.axisminimum);
            //设置字体颜色
            setting.yAxis.setTextColor(this.textcolor);
            //Y轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题
            setting.yAxis.setLabelCount(this.labelcount,this.labelforce);
            //是否隐藏右边的Y轴（不设置的话有两条Y轴 同理可以隐藏左边的Y轴）
            setting.defBarChart.getAxisRight().setEnabled(this.axisrightenabled);
        }
    }
}
